package com.slkj.shilixiaoyuanapp.ui.teacher.tool.StudentAttendance;

import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.entity.MainToolEntity;
import com.slkj.shilixiaoyuanapp.entity.StudentAttendanceEntity;
import com.slkj.shilixiaoyuanapp.entity.StudentAttendanceListEntity;
import com.slkj.shilixiaoyuanapp.view.CustomStateText;

import java.util.List;

/**
 * Created  on 2018-09-07
 *
 * @author 唐开阔
 * @describe 考勤列表适配器
 */
public class AttendanceListAdaper extends BaseMultiItemQuickAdapter<StudentAttendanceEntity.StudentEntity, BaseViewHolder> {
    List<StudentAttendanceEntity.StudentEntity> data;

    public AttendanceListAdaper(List<StudentAttendanceEntity.StudentEntity> data) {
        super(data);
        this.data = data;
        addItemType(StudentAttendanceEntity.StudentEntity.TYPE_LEAVE, R.layout.item_student_attendance_leave);
        addItemType(StudentAttendanceListEntity.TYPE_KQ, R.layout.item_student_attendance_kq);
    }

    @Override
    protected void convert(BaseViewHolder helper, StudentAttendanceEntity.StudentEntity item) {
        if (getItemViewType(helper.getAdapterPosition()) == StudentAttendanceListEntity.TYPE_LEAVE) {
            bindLeaveData(helper.getAdapterPosition(), helper, item);
        } else if (getItemViewType(helper.getAdapterPosition()) == StudentAttendanceListEntity.TYPE_KQ) {
            bindKqData(helper.getAdapterPosition(), helper, item);
        }
    }

    /**
     * 绑定考勤数据
     * @param adapterPosition
     * @param helper
     * @param item
     */
    private void bindKqData(int adapterPosition, BaseViewHolder helper, StudentAttendanceEntity.StudentEntity item) {
        View lastView = helper.getView(R.id.view_last);
        ImageView img = helper.getView(R.id.img_orientation);
        img.setImageResource(!item.isOpen() ? R.drawable.ic_down : R.drawable.ic_up);
        lastView.setVisibility(item.isOpen() ? View.VISIBLE : View.GONE);
        helper.setText(R.id.tv_name, item.getName())
                .setText(R.id.tv_type,item.getTypeStr())
                .getView(R.id.layout_top)
                .setOnClickListener(v -> {
                    item.setOpen(!item.isOpen());
                    notifyItemChanged(adapterPosition);
                });
        CustomStateText customStateText = helper.getView(R.id.view_state);
        customStateText.setSolidColor(item.getTypeColor());
        helper.getView(R.id.layout_zc).setOnClickListener(v -> {
            item.setType(1);
            notifyItemChanged(adapterPosition);
        });
        helper.getView(R.id.layout_cd).setOnClickListener(v -> {
            item.setType(2);
            notifyItemChanged(adapterPosition);
        });
    }

    /**
     * 绑定请假数据
     *
     * @param adapterPosition
     * @param helper
     * @param item
     */
    private void bindLeaveData(int adapterPosition, BaseViewHolder helper, StudentAttendanceEntity.StudentEntity item) {
        /**
         * 判断是否是最后一条请假数据：
         * 1：列表最后一条数据
         * 2：下一条数据是考勤数据
         */
        boolean isLast = adapterPosition == data.size() - 1 || data.get(adapterPosition + 1).getItemType() == StudentAttendanceListEntity.TYPE_KQ;
        helper.setText(R.id.tv_name, item.getName()).getView(R.id.view_last).setVisibility(isLast ? View.VISIBLE : View.GONE);
    }
}
